import { defineStore } from 'pinia'
import $ajaxGrid from '../api/to8082.js'
import $ajaxFeedback from '../api/to8083.js'
import $ajaxAqi from '../api/to8084.js'
import moment from 'moment'
import { alertMsg } from '@/utils/msg.js'
import { useRoute, useRouter } from 'vue-router'
import router from '@/router/index'

export const Publics = defineStore('publics', {
  state: () => ({
    /** @type { value:number,label:String,children:list} */
    cascader: [],
    place: [],
    loc: '',
    remark: '',
    aqis: [],
    curAqi: '',
    feedback: {
      pid: '',
      fpid: '',
      fcid: '',
      frid: '',
      floc: '',
      aid: '',
      remark: ''
    },
    pid: 0,
    his: [],
    dialogShow: false,
    curRow: -1,
    curLog: '',
    curInfo: '',
    infos: [],
    timeline: [],
    show: false,
    showName: ''
  }),
  getters: {
    filterPublics(state) {
      this.loadData()
      return state.cascader
    }
  },
  // 也可以这样定义
  // state: () => ({ count: 0 })
  actions: {
    loadId(id) {
      this.pid = id
    },
    loadData() {
      this.loadPCR()
      this.loadAQI()
      this.loadHis()
      this.loadInfo()
      this.loadTimeLine()
    },
    async loadPCR() {
      const path = '/grid/list'

      const res = await $ajaxGrid.get(path)

      if (res.code === 200) this.cascader = res.data
    },
    async loadAQI() {
      const path = '/aqi/list'

      const res = await $ajaxAqi.get(path)

      if (res.code === 200) this.aqis = res.data
    },
    printData() {
      console.log(this.place)
    },
    goback() {
      router.push({ name: 'public', query: { id: this.pid } })
    },
    cancel() {},
    done() {
      this.feedback = {
        pid: this.pid,
        fpid: this.place[0],
        fcid: this.place[1],
        frid: this.place[2],
        floc: this.loc,
        aid: this.curAqi,
        remark: this.remark
      }

      this.addFeedback()
    },
    async addFeedback() {
      const path = '/feedback/p/add'
      const res = await $ajaxFeedback.post(path, this.feedback)
      alertMsg(res.code, res.msg)
    },
    async loadHis() {
      const path = '/feedback/p/history/' + this.pid
      const res = await $ajaxFeedback.get(path)
      if (res.code === 200) this.his = res.data
      console.log(this.his)
    },
    showResult(id) {
      this.curRow = id
      this.curLog = this.his[this.curRow]
      this.loadInfoById(this.curLog.id)
      console.log(this.curLog.fiid)
      this.curInfo = this.infos.find((item) => item.id === this.curLog.fiid)
      this.dialogShow = true
      console.log(this.curInfo)
    },
    async loadInfoById(id) {
      const path = '/feedback/p/info/' + id
      const res = await $ajaxFeedback.get(path)
      if (res.code === 200) this.curInfo = res.data
      console.log(this.curInfo)
    },
    async loadInfo() {
      const path = '/feedback/p/info'
      const res = await $ajaxFeedback.get(path)
      if (res.code === 200) this.infos = res.data
    },
    async loadTimeLine() {
      console.log(this.pid)
      const path = '/feedback/p/timeline/' + localStorage.getItem('pid')
      const res = await $ajaxFeedback.get(path)
      // res.data.map((row) => {
      //   row.time = new Date(row.time).toLocaleString()
      //   return row.time
      // })
      if (res.code === 200) this.timeline = res.data
      console.log('timeline 0 ', this.timeline[0])
    },
    onShow() {
      this.show = true
    },
    onFinish(name, ids) {
      this.place = ids
      this.showName = name
      this.show = false
    },
    onClose() {
      this.show = false
    }
  }
})
